package com.pagoda.nerp.web.services;

import com.alibaba.fastjson.parser.deserializer.AbstractDateDeserializer;
import com.pagoda.nerp.web.common.buildTree.Tree;
import com.pagoda.nerp.web.common.buildTree.TreeBuild;
import com.pagoda.nerp.web.entities.HRORGPost;
import com.pagoda.nerp.web.entities.HRUnit;
import com.pagoda.nerp.web.repositories.HRORGPostDao;
import com.pagoda.nerp.web.repositories.HRUnitDao;
import com.pagoda.nerp.webapi.hander.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HRORGPostService {
    @Autowired
    private HRORGPostDao hrorgPostDao;
    @Autowired
    private HRUnitDao hrUnitDao;
    @Autowired
    private EHRService ehrService;
    @Autowired
    private HRUnitService hrUnitService;

    /**
    *   @Description:  查询树形数据
    *   @Parm: 根节点的postid
    *   @Date: 10:31 2017/12/27
    */
    public ResponseResult getHRORGPostTree(String rootid){
        //获取到根节点信息
        Long start = System.currentTimeMillis();
        HRORGPost hrorgPost = hrorgPostDao.findOne(rootid);
        //获取更节点的树编码
        String canonicalCode = hrorgPost.getCanonicalcode();
        //通过树编码查询到以此为根的树的数据，数据有序(canonicalcode+canonicalcode长度)
        List<HRORGPost> list = hrorgPostDao.findData(canonicalCode);
        Long end = System.currentTimeMillis();
        System.out.println("find:"+(end-start));

        start = System.currentTimeMillis();
        List<Tree> answer = TreeBuild.buildTree(list);
        end = System.currentTimeMillis();
        System.out.println("build:"+(end - start));
        return ResponseResult.success(answer);
    }

    /**
     * @Description: 更新post表
     * @Date: 11:14 2018/1/9
     */
    public List<HRORGPost> updateDBPost(){
        Date modifydate = getModifyTime();
        List<HRORGPost> list = getUpdateList(modifydate);
        List<HRUnit> unitList = hrUnitService.getUpdateList(modifydate);
        for (HRORGPost hrorgPost : list) {
//            System.out.println(hrorgPost.toString());
            HRUnit dept = hrUnitDao.findByOrgcode(hrorgPost.getDeptcode());
            hrorgPost.setDeptid(dept.getOrguid());
            hrorgPost.setCanonicalparentcode(dept.getCanonicalcode());
            hrorgPost.setCanonicalcode(dept.getCanonicalcode()+"/"+hrorgPost.getPostid());
            hrorgPost.setModifytime(Calendar.getInstance().getTime());
        }
        for (HRUnit hrUnit : unitList){
            HRUnit parentnm = hrUnitDao.findOne(hrUnit.getParentnm());
            if(parentnm == null){
                hrUnit.setCanonicalcode(hrUnit.getOrguid());
            }
            else {
                hrUnit.setCanonicalparentcode(parentnm.getCanonicalcode());
                hrUnit.setCanonicalcode(parentnm.getCanonicalcode() + "/" + hrUnit.getOrguid());
            }
            HRORGPost hrorgPost = transformToHROrgpost(hrUnit);
            list.add(hrorgPost);
        }
        hrorgPostDao.save(list);
        modifyUpdateTime();
        System.out.println("UpdateDBPost Complete!");
        return list;
    }

    public void save(List<HRORGPost> HRORGPostList) {
        hrorgPostDao.save(HRORGPostList);
    }

    /**
     * @Description:
     * @Param:
     * @Date: 14:45 2018/1/4
     */
    public Date getModifyTime(){
        HRORGPost hrorgPost = hrorgPostDao.findOne("updatetime");
        //return hrorgPost.getModifytime();
        return hrorgPost == null ? new Date(1525276800000L): hrorgPost.getModifytime();

    }

    /**
     * @Description: 得到更新的列表
     * @Date: 17:09 2018/1/8
     */
    public List<HRORGPost> getUpdateList(Date modifydate){
        List<HRORGPost> list = new ArrayList<>();

        Calendar current=Calendar.getInstance();
        Calendar modify = Calendar.getInstance();
        modify.setTime(modifydate);

        //循环日期，获取数据只能获取到一天的数据
        while (modify.before(current))
        {
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            String date = df.format(modify.getTime());
            System.out.println("HRORGPostService.getUpdateList " + date);
            List<HRORGPost> add = ehrService.getHRORGPostList(date);
            if(add !=null) list.addAll(add);
            modify.add(Calendar.DAY_OF_YEAR,1);
        }
        return list;
    }

    /**
     * @Description:修改最后更新时间
     * @Date: 17:09 2018/1/8
     */
    public void modifyUpdateTime(){
        Calendar calendar = Calendar.getInstance();
        HRORGPost hrorgPost = hrorgPostDao.findOne("updatetime");
        hrorgPost.setModifytime(calendar.getTime());
        hrorgPostDao.save(hrorgPost);
    }

    /**
     * @Description: 添加机构树编码
     * @Date: 11:02 2018/1/9
     */
    public void addCanonicalcode() {
        List<HRORGPost> alllist = hrorgPostDao.findAll();
        for (HRORGPost hrorgPost : alllist) {
            if(hrorgPost.getPostid().equals("updatetime")) continue;
            String deptcode = hrorgPost.getDeptcode();
            HRUnit hrUnit = hrUnitDao.findByOrgcode(deptcode);
            hrorgPost.setDeptid(hrUnit.getOrguid());
            hrorgPost.setCanonicalparentcode(hrUnit.getCanonicalcode());
            hrorgPost.setCanonicalcode(hrUnit.getCanonicalcode() + "/" + hrorgPost.getPostid());
            hrorgPost.setModifytime(Calendar.getInstance().getTime());
        }
        hrorgPostDao.save(alllist);
    }

    /**
     *  @Description: 添加hrunit数据到hrpost
     *  @Pram:
     *  @Date: 10:39 2017/12/27
     */
    public void addDataFromHRUnit(){
        List<HRUnit> list = hrUnitDao.findAll();
        List<HRORGPost> list_post = new ArrayList<>();
        for (HRUnit hrUnit:list){
            if(hrUnit.getOrguid().equals("updatetime")) continue;
            //信息转化
            HRORGPost hrorgPost = transformToHROrgpost(hrUnit);
            list_post.add(hrorgPost);
        }
        hrorgPostDao.save(list_post);
    }
    /**
     * @Description: hrunit 转化为 HROrgpost
     * @Param: hrunit实体
     * @Date: 11:18 2018/1/9
     */
    public HRORGPost transformToHROrgpost(HRUnit hrUnit){
        HRORGPost hrorgPost = new HRORGPost();
        hrorgPost.setPostid(hrUnit.getOrguid());
        hrorgPost.setJobtitlename(hrUnit.getOrgname());
        hrorgPost.setCanonicalcode(hrUnit.getCanonicalcode());
        hrorgPost.setCanonicalparentcode(hrUnit.getCanonicalparentcode());
        hrorgPost.setModifytime(Calendar.getInstance().getTime());
        return hrorgPost;
    }

}
